package com.helloUIBinder.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.helloUIBinder.client.receivers.BlogPostReceiver;
import com.helloUIBinder.client.ClientPropertiesLoader;
import com.helloUIBinder.client.ProjectJsonParser;
import com.helloUIBinder.client.Utils;
import com.helloUIBinder.client.events.OnPageClickEvent;
import com.helloUIBinder.client.events.OnPageClickHandler;
import com.helloUIBinder.model.BlogPost;

import java.util.ArrayList;
import java.util.List;

public class BlogPageWidget extends Composite {

    private static BlogWidgetUiBinder uiBinder = GWT.create(BlogWidgetUiBinder.class);

    interface BlogWidgetUiBinder extends UiBinder<Widget, BlogPageWidget> {
    }

    @UiField
    HTMLPanel blogAnnotationContainer;

    protected SimpleEventBus eventBus = new SimpleEventBus();

    private Integer currentPage;
    private Integer pageSize;
    private PageChooserWidget currentPageChooserWidget;
    private List<BlogAnnotationWidget> currentDisplayedBlogAnnotationWidgets = new ArrayList<BlogAnnotationWidget>();


    public BlogPageWidget() {
        initWidget(uiBinder.createAndBindUi(this));
        fooRunner();
        currentPage = 1;

        BlogPostReceiver.getAllBlogPostsSize(getBlogPostSizeCallBack());
        BlogPostReceiver.findAllBlogPostsByPage(getBlogPostCallBack(), currentPage - 1, 4, null, null); //pages in DB counts from 0

        eventBus.addHandler(OnPageClickEvent.TYPE, new OnPageClickHandler() {
            @Override
            public void click(OnPageClickEvent event) {
                String clickedLinkName = event.getClickedLinkName();
                if (clickedLinkName.equals("Next")) {
                    currentPage++;
                    if(currentPage > pageSize){
                        currentPage = pageSize;
                    }
                }
                if (clickedLinkName.equals("Prev")) {
                    currentPage--;
                    if(currentPage < 1){
                        currentPage = 1;
                    }
                }
                if (!clickedLinkName.equals("Prev") && !clickedLinkName.equals("Next")) {
                    currentPage = Integer.parseInt(clickedLinkName);
                }
                BlogPostReceiver.findAllBlogPostsByPage(getBlogPostCallBack(), currentPage - 1, 4, null, null); //pages in DB counts from 0
            }
        });

    }

    private RequestCallback getBlogPostCallBack() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                List<BlogPost> blogPostList = ProjectJsonParser.parseJsonArray(BlogPost.class, response.getText());

                List<BlogAnnotationWidget> widgetList = Utils.getBlogPostWidgetsFromEntities(blogPostList);

                if (currentDisplayedBlogAnnotationWidgets.size() > 0) {
                    for (BlogAnnotationWidget curreWidget : currentDisplayedBlogAnnotationWidgets) {
                        blogAnnotationContainer.remove(curreWidget);
                    }
                }
                if (currentPageChooserWidget != null) {
                    blogAnnotationContainer.remove(currentPageChooserWidget);
                }
                for (BlogAnnotationWidget currentWidget : widgetList) {
                    blogAnnotationContainer.add(currentWidget);
                    currentDisplayedBlogAnnotationWidgets.add(currentWidget);
                }

                currentPageChooserWidget = new PageChooserWidget(pageSize, eventBus, currentPage);
                blogAnnotationContainer.add(currentPageChooserWidget);
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    private RequestCallback getBlogPostSizeCallBack() {
        return new RequestCallback() {
            @Override
            public void onResponseReceived(Request request, Response response) {
                Float blogPostCount = Float.parseFloat(response.getText());
                pageSize = (int)(Math.ceil(blogPostCount / ClientPropertiesLoader.getBlogPagesCount()));
            }

            @Override
            public void onError(Request request, Throwable exception) {
                Window.alert("Error occurred" + exception.getMessage());
            }
        };
    }

    public static native void fooRunner() /*-{
        $doc.fooRunner();
    }-*/;
}
